#ifndef __WIZ_ATOMIC_COUNT_HPP__SHANHAOBO_19800429__
#define __WIZ_ATOMIC_COUNT_HPP__SHANHAOBO_19800429__

#include "../Base/WizBase.hpp"

namespace Wiz
{
    namespace AtomicCount
    {
        struct Interface
        {
            virtual ::Wiz::Int::Type Count() = 0;
            virtual ::Wiz::Void::Type Increment() = 0;
            virtual ::Wiz::Int::Type  Decrement() = 0;
            virtual ::Wiz::Int::Type  CompareSwap(::Wiz::Int::In compare_, ::Wiz::Int::In swap_) = 0;
            ::Wiz::Bool::Type DecIsZero()
            {
                return this->Decrement() == 0;
            }
            ::Wiz::Void::Type CASInc()
            {
                for(;;)
                {
                    ::Wiz::Int::Type tmp = this->Count();
                    if (this->CompareSwap(tmp, tmp + 1) == tmp)
                    {
                        return;
                    }
                }
            }
            ::Wiz::Int::Type SafeCount()
            {
                this->Increment();
                return this->Decrement();
            }
        };
        WIZ_DECLARE_PTR(::Wiz::AtomicCount::Interface)

        WIZ_EXPORT ::Wiz::AtomicCount::Ptr Create();
        WIZ_EXPORT ::Wiz::Void::Type Destroy(::Wiz::AtomicCount::Ptr&);
        class CreateFailed : public ::Wiz::Exception::Type
        {
            typedef ::Wiz::Exception::Type  tSuper;
        public:
            CreateFailed() : tSuper("Create Atomic Count Failed")
            {}
        public:
        };
    } /// end of namespace AtomicCount
} /// end of namespace Wiz

#endif /*__WIZ_ATOMIC_COUNT_HPP__SHANHAOBO_19800429__*/
